<template>
  <div class="box">
    <div class="nav">
      <ul class="location">
        <li>
        </li>
      </ul>
      <ul class="detail">
        <!-- 登录 -->
        <li class="first">
          <!--          你好，<router-link to="/login">{{(account=this.$route.query.account) == undefined?'请登录':account}} <Icon type="person"></Icon></router-link>-->
          你好，<span @click="Login">{{ userName }} <Icon type="person"></Icon></span>
          <!--          你好，<router-link to="/login">{{customer.account == ''?'请登录':customer.account}} <Icon type="person"></Icon></router-link>-->
        </li>
        <li v-show="!!this.$route.query.account">
          <Dropdown>
            <p class="username-p">
              <Avatar class="person-icon" icon="person" size="small" /> <span class="username"></span>

            </p>
            <DropdownMenu slot="list">
              <div class="my-page">
                <div class="my-info" @click="setUser">
                  <Icon type="home"></Icon>
                  <p>我的主页</p>
                </div>

                <div class="sign-out" @click="logout">
                  <Icon type="log-out"></Icon>
                  <p @click="logout">退出登录</p>
                </div>
              </div>
            </DropdownMenu>
          </Dropdown>
        </li>
        <li>
          <Dropdown placement="bottom-start">
            <a href="javascript:void(0)">
              <Icon type="ios-cart-outline"></Icon>
              <span @click="goToPay">
                购物车
              </span>
            </a>

          </Dropdown>
        </li>
        <!--        <li><router-link to="/freeback">意见反馈</router-link></li>-->
        <!--        <li><router-link to="/">商城首页</router-link></li>-->
        <li><a><span @click="ToHome">商城首页</span></a></li>
      </ul>
    </div>
  </div>
</template>

<script>
import store from '@/vuex/store';
import { mapState, mapActions } from 'vuex';
import { userInfo } from '@/api/user.js';
import { uesrLogout } from '../../api/user';
export default {
  name: 'M-Header',
  created() {
    this.setUserInfo();
  },
  data() {
    return {
      show_logout: true,
      customer: {
        account: '',
        id: ''
      }
    };
  },
  computed: {
    ...mapState(['userInfo', 'shoppingCart', 'userName'])
  },
  watch: {
    customer: function (val) {
      // this.setUserInfo();
    }
  },
  methods: {
    ...mapActions(['signOut']),
    // 初始化数据
    // 判断用户是否登录，若已登录，则无任何操作；否则，转到login界面
    Login() {
      console.log(this.$route.query.account);
      console.log(this.customer.account);
      if (this.$route.query.account === undefined) { this.$router.push('/login'); }
    },
    setUserInfo() {
      userInfo().then(res => {
        if (res.data.code === '00000') {
          this.customer = res.data.data;
        }
      }).catch(error => {
        this.$Message.error(error.message);
      });
    },
    goToPay() {
      this.$router.push({
        path: '/Order',
        query: { account: this.account }
      });
    },
    setUser() {
      this.$router.push({
        path: '/home',
        query: { account: this.account }
      });
    },
    logout() {
      // 清空sessonStorage
      // TODO URL API DELETE
      window.sessionStorage.clear();
      // eslint-disable-next-line no-unused-expressions
      // 重定向到login页面
      this.$store.commit('setUserName', '请登录');
      // this.customer.account = undefined;
      // this.customer.id = undefined;
      // console.log(this.customer.account);
      // console.log(this.customer.id);
      this.$router.push('/login');
    },
    signOutFun() {
      // const father=this;
      // 要删除用户的cookie缓存

      console.log(this.$route.query.account);
      console.log(1);
      console.log(2);
      console.log('来这里了嘛1');
      uesrLogout().then(res => {
        console.log('来这里了嘛2');
        console.log(res.data.data);
        if (res.data.code === '00000') {
          console.log('来这里了嘛3');
          window.sessionStorage.clear();
          this.account = '';
          // 重定向到商城首页页面
          console.log('来这里了嘛4');
          this.$router.push('/');
          console.log('来这里了嘛5');
        } else {
          console.log('来这里了嘛6');
          this.$Message.error(res.data.message);
          console.log(res.data.message);
        }
      }
      ).catch(error => {
        console.log('来这里了嘛7');
        this.$Message.error(error.message);
        console.log(error.message);
      });
    },
    // 回到商城首页
    ToHome() {
      this.$router.push({
        path: '/',
        query: { account: this.account }
      });
    }
    // signOutFun () {
    //   // const father=this;
    //   // 要删除用户的cookie缓存
    //   console.log(this.$route.query.account);
    //   this.signOut();
    //   this.$router.push('/');
    // }
    // TODO URL API DELETE
    // 向服务器发出退出登录请求
    //   uesrLogout(this.id).then(res => {
    //     if (res.date.code === '00000') {
    //       // message消息提示
    //       this.$Message.success(res.data.message);
    //       window.sessionStorage.clear();
    //       // 重定向到商城首页页面
    //       this.$router.push('/');
    //     } else {
    //       this.$Message.error(res.data.message);
    //     }
    //   }
    //   ).catch(error => {
    //     this.$Message.error(error.message);
    //     this.$Message.error('你的id不对哦');
    //   });
    // }
    // 退出登录这一span的显示和隐藏
    // changeStatus () {
    //   if (this.$route.query.account !== 'undefined') {
    //     this.show_logout = true;
    //   }
    // }
  },
  store
};
</script>

<style scoped>
.box {
  width: 100%;
  height: 35px;
  background-color: #e3e4e5;
}

.nav {
  margin: 0% auto;
  width: 90%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.nav ul {
  list-style: none;
}

.nav li {
  float: left;
  font-size: 14px;
  line-height: 35px;
  margin-right: 15px;
  font-weight: bold;
}

.nav a {
  text-decoration: none;
  color: #999999;
  padding-left: 15px;
  border-left: 1px solid #ccc;
  cursor: pointer;
}

.location a {
  border-left: none;
}

.nav a:hover {
  color: #d9534f;
}

.location {
  color: #999999;
}

.icon {
  color: #d9534f;
}

.first {
  color: #999999;
}

.first a:first-child {
  padding-left: 3px;
  border-left: none;
}

.person-icon {
  color: #d9534f;
  background-color: #f0cdb2;
}

.username {
  color: #999999;
}

.shopping-cart-list {
  padding: 3px 15px;
}

.shopping-cart-box {
  margin: 8px 0px;
  margin-top: 15px;
  padding-bottom: 15px;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px #ccc dotted;
}

.shopping-cart-box:first-child {
  margin-top: 8px;
}

.shopping-cart-img {
  margin-right: 15px;
  width: 40px;
  height: 40px;
}

.shopping-cart-img img {
  width: 100%;
}

.shopping-cart-info {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-content: space-between;
  width: 200px;
  overflow: hidden;
  font-size: 12px;
  line-height: 20px;
  color: #999999;
}

.shopping-cart-detail {
  color: #999999;
}

.shopping-cart-text {
  color: #ccc;
}

.go-to-buy {
  display: flex;
  justify-content: flex-end;
}

.shopping-cart-null {
  padding: 15px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.cart-null-icon {
  font-size: 38px;
  margin-bottom: 15px;
}

.shopping-cart-null span {
  color: #999999;
  font-size: 12px;
  line-height: 16px;
}

.username-p {
  cursor: pointer;
}

.my-page {
  padding: 3px 5px;
  width: 180px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.my-page a {
  margin: 0px;
  padding: 0px;
  border: none;
}

.my-info {
  padding: 5px;
  width: 50%;
  height: 100%;
  text-align: center;
  cursor: pointer;
}

.my-info:hover {
  box-shadow: 0px 0px 5px #ccc;
}

.my-info i {
  font-size: 28px;
}

.my-info p {
  font-size: 12px;
}

.sign-out {
  padding: 5px;
  width: 50%;
  height: 100%;
  text-align: center;
  cursor: pointer;
}

.sign-out:hover {
  box-shadow: 0px 0px 5px #ccc;
}

.sign-out i {
  font-size: 28px;
}

.sign-out p {
  font-size: 12px;
}

span {
  margin-left: 15px;
}
</style>
